<template>
    <form-container :title="formData.id ? '编辑车间信息' : '新增车间信息'" :width="550" :visible="visible" :destroy-on-close="true"
        @close="onClose">
        <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
            <a-form-item label="编码" name="id">
                <a-input v-model:value="formData.id" allow-clear />
            </a-form-item>
            <a-form-item label="工单" name="planId">
                <a-select v-model:value="formData.planId" placeholder="请选择">
                    <a-select-option v-for="item in productPlan" :key="item.id" :value="item.id">{{ item.id
                        }}</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="员工" name="employeeCode">
                <a-select v-model:value="formData.employeeCode" placeholder="请选择">
                    <a-select-option v-for="item in employee" :key="item.employeeCode" :value="item.employeeCode">{{
                        item.employeeName }}</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="工序" name="processCode">
                <a-select v-model:value="formData.processCode" placeholder="请选择产线">
                    <a-select-option v-for="item in process" :key="item.processCode" :value="item.processCode">{{
                        item.processName
                        }}</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="实际生产数量" name="actualQty">
                <a-input v-model:value="formData.actualQty" placeholder="请输入名称" allow-clear />
            </a-form-item>
            <a-form-item label="报工时间" name="reportTime">
                <a-date-picker v-model:value="formData.reportTime" show-time type="date" placeholder="Pick a date"
                    style="width: 100%" />
            </a-form-item>
            <a-form-item label="状态" name="status">
                <a-select v-model:value="formData.status" placeholder="请选择产线">
                    <a-select-option :value="1">审核中</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="审核信息" name="auditRemark">
                <a-input v-model:value="formData.auditRemark" placeholder="请输入名称" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-checkbox class="mr-2" v-if="!formData.id" v-model:checked="addNext">继续新增下一条</a-checkbox>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
        </template>
    </form-container>
</template>

<script setup name="positionForm">
import { required } from '@/utils/formRules'
import { getCurrentInstance } from 'vue'
import { message } from 'ant-design-vue'
import tool from '@/utils/tool'
import productionApi from '@/api/production/ticketManagementaApi'
import workReporterApi from '@/api/production/workReporterApi'
import employeeApi from '@/api/production/employeeApi'
import processApi from '@/api/mes/processinfo/processApi'

// 定义emit事件
const emit = defineEmits({ successful: null })
const { proxy } = getCurrentInstance()
// 默认是关闭状态
let visible = ref(false)
const formRef = ref()
// 表单数据，也就是默认给一些数据
const formData = ref({})
// 定义机构元素
const treeData = ref([])
const submitLoading = ref(false)
// 继续新增下一条
const addNext = ref(false)

// 打开抽屉
const onOpen = (record, orgId) => {
    visible.value = true
    addNext.value = false
    formData.value = {
        id: formData.value.id,
        productCode: formData.value.productCode,
        productName: formData.value.productName,
        productionLineCode: formData.value.productionLineCode,
        plannedQty: formData.value.plannedQty,
        isSkipProcess: formData.value.isSkipProcess,
        annex: formData.value.annex,
        remark: formData.value.remark,
        startTime: formData.value.startTime,
        endTime: formData.value.endTime,
        status: formData.value.status
    }
    if (record) {
        formData.value = Object.assign({}, record)
    }

}
// 关闭抽屉
const onClose = () => {
    visible.value = false
}
// 默认要校验的
const formRules = {
    sName: [required('请输入学生名称')],
    age: [required('请输入年龄')],
}
//类型
const processTypeOptions = tool.dictList('PROCESS_TYPE')
// 报工模式
const processModeOptions = tool.dictList('PROCESS_MODE')


// 验证并提交数据
const onSubmit = () => {
    formRef.value.validate().then(() => {
        submitLoading.value = true
        workReporterApi
            .addWorkReporter(formData.value, formData.value.id)
            .then(() => {
                if (addNext.value) {
                    formData.value = {
                        id: formData.value.id,
                        productCode: formData.value.productCode,
                        productName: formData.value.productName,
                        productionLineCode: formData.value.productionLineCode,
                        plannedQty: formData.value.plannedQty,
                        isSkipProcess: formData.value.isSkipProcess,
                        annex: formData.value.annex,
                        remark: formData.value.remark,
                        startTime: formData.value.startTime,
                        endTime: formData.value.endTime,
                        status: formData.value.status
                    }
                } else {
                    visible.value = false
                }
                emit('successful')
            })
            .finally(() => {
                submitLoading.value = false
            })
    })
}
// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
    onOpen
})

//获取工单
const productPlan = ref([])
const queryProductPlan = () => {
    productionApi.bindTicket({}).then(res => {
        productPlan.value = res
    })
}

//获取员工
const employee = ref([])
const queryEmployee = () => {
    employeeApi.bindEmployee({}).then(res => {
        employee.value = res
    })
}

//获取工序
const process = ref([])
const queryProcess = () => {
    processApi.bindProcess({}).then(res => {
        process.value = res
    })
}

onMounted(() => {
    queryProductPlan();
    queryEmployee();
    queryProcess();
})
</script>
